package com.cn;

import org.flowable.engine.RepositoryService;
import org.flowable.engine.RuntimeService;
import org.flowable.engine.TaskService;
import org.flowable.engine.repository.Deployment;
import org.flowable.engine.runtime.ProcessInstance;
import org.flowable.task.api.Task;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 创建文件方式
 */
@SpringBootTest
public class flowableTest08 {

    @Autowired
    private RepositoryService repositoryService;

    @Autowired
    private RuntimeService runtimeService;

    @Autowired
    private TaskService taskService;
    /**
     * 启动流程部署的方式
     * c9afb6d0-b12b-11ee-8db7-30d04231d1ff
     */
    @Test
     void name() {
        Deployment deploy = repositoryService.createDeployment()
                .addClasspathResource("process01-基础/学生管理系统.bpmn20.xml")
                .name("学生管理系统01")
                .deploy();
        System.out.println(deploy.getId());
    }


    /**
     * 出差申请的方式
     *
     * e48586a6-b12b-11ee-ab93-30d04231d1ff
     */
    @Test
    void testController(){
        String userId = "userId:test";
        Integer money= 500;
        String descption = "";
        Map<String,Object> map = new HashMap<>();
        map.put("taskUser",userId);
        map.put("money",money);
        ProcessInstance expese = runtimeService.startProcessInstanceByKey("出差申请我的方式", map);
        System.out.println("expese.getId()"+expese.getId());
    }

    /**
     * 获取审批列表
     *
     */
    @Test
    void shenpiList(){
        String id = "e48586a6-b12b-11ee-ab93-30d04231d1ff";
        List<Task> list = taskService.createTaskQuery()
                .taskAssignee(id)
                .orderByTaskCreateTime()
                .desc()
                .list();
        for (Task task : list) {
            System.out.println("task="+task.toString());
        }

    }

    @Test
    void testStudent() {
        /*Student student = new Student();
        student.setName("天台你");
        student.setDay(2);
        student.setDescription("操");
        Map<String,Object> map = new HashMap<>();
        map.put("name",student.getName());
        map.put("day",student.getDay());
        map.put("description",student.getDescription());
        map.put("stu","学生01");
        map.put("stu01","学生02");
        map.put("stu02","学生03");

        //启动任务
        ProcessInstance processInstance =
                runtimeService.startProcessInstanceByKey("学生管理系统", map);*/

        //根据名称查询受理人任务的方式
//        List<Task> list = taskService.createTaskQuery()
//                .taskAssignee("学生01")
//                .list();
//        System.out.println(list);

//        Map<String,Object> map = new HashMap<>();
//        map.put("lao","张老师");
//        map.put("lao01","王老师");
//        map.put("lao02","情爱老师");
//        taskService.complete("b2a89689-b381-11ee-9f42-30d04231d1ff",map);

        List<Task> list = taskService.createTaskQuery()
                .taskAssignee("张老师")
                .list();
        System.out.println(list);

        //根据变量名称获取方式
//        Map<String, Object> map = taskService.getVariables("b2a89689-b381-11ee-9f42-30d04231d1ff");
//        System.out.println(map);


        //查询待测任务
      /*  List<Task> list = taskService.createTaskQuery()
                .taskAssignee(student.getName())
                .list();
        System.out.println(list);*/

    }


    /**
     * 排他网关设置方式
     */
    @Test
    void testGateway() {
        String taskId = "0989b5dc-b38b-11ee-8754-30d04231d1ff";
        Map<String,Object> map = new HashMap<>();
        map.put("command","refuse");
        taskService.complete(taskId,map);
    }

    @Test
    void startTask() {
        String taskId = "0bc36fcc-b3a4-11ee-b3b2-30d04231d1ff";
        Map<String,Object> map = new HashMap<>();
        //学生管理
//        map.put("stu","refuse1");
//        map.put("stu01","refuse01");
//        map.put("stu02","refuse02");
//        map.put("command","refuse");
        //老师
//        map.put("lao","refuse1");
//        map.put("lao01","refuse01");
//        map.put("lao02","refuse02");
        map.put("command","agree");
        map.put("xiao","t");
        map.put("xiao02","r");
        map.put("xiao03","x");
        taskService.complete(taskId,map);
    }
}